package com.sk.bilibili.callBack;

import android.view.View;

import com.google.android.material.appbar.AppBarLayout;

public class OnAppBarChangeListener implements  AppBarLayout.BaseOnOffsetChangedListener<AppBarLayout>{

    private AppBarChangeCallBack appBarChangeCallBack;

    public OnAppBarChangeListener(AppBarChangeCallBack appBarChangeCallBack) {
        this.appBarChangeCallBack = appBarChangeCallBack;
    }

    private CollapsingToolbarLayoutState state;

    private enum CollapsingToolbarLayoutState {
        EXPANDED,
        COLLAPSED,
        INTERNEDIATE
    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if (verticalOffset == 0) {
            if (state != CollapsingToolbarLayoutState.EXPANDED) {
                state = CollapsingToolbarLayoutState.EXPANDED;//修改状态标记为展开
                appBarChangeCallBack.onExpand();
            }
        } else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {
            if (state != CollapsingToolbarLayoutState.COLLAPSED) {
                state = CollapsingToolbarLayoutState.COLLAPSED;//修改状态标记为折叠
                appBarChangeCallBack.onClose();
            }
        } else {
            if (state != CollapsingToolbarLayoutState.INTERNEDIATE) {
                if (state == CollapsingToolbarLayoutState.COLLAPSED) {
                    appBarChangeCallBack.onExpanding();
                }else {
                    appBarChangeCallBack.onClosing();
                }
                state = CollapsingToolbarLayoutState.INTERNEDIATE;//修改状态标记为中间
            }
        }
    }

}